using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FaceCat;

namespace facecat
{
    /// <summary>
    /// 视图
    /// </summary>
    public class FCUIView : GraphicsView
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FCUIView()
        {
            onLoad();
            this.StartInteraction += FCUIView_StartInteraction;
            this.EndInteraction += FCUIView_EndInteraction;
            this.MoveHoverInteraction += FCUIView_MoveHoverInteraction;
        }

        public Entry m_textBox;

        /// <summary>
        /// 虚拟输入框
        /// </summary>
        public FCView m_editingTextBox;

        /// <summary>
        /// 触摸移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FCUIView_MoveHoverInteraction(object? sender, TouchEventArgs e)
        {
            m_host.m_touchPoint = new FCPoint(e.Touches[0].X, e.Touches[0].Y);
            if (m_native.m_touchDownView != null)
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = 1;
                m_native.onMouseMove(newTouchInfo);
            }
            else
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                m_native.onMouseMove(newTouchInfo);
            }
        }

        public DateTime m_lastClickLeft;
        public bool m_isDoubleClickLeft;

        /// <summary>
        /// 触摸结束事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FCUIView_EndInteraction(object? sender, TouchEventArgs e)
        {
            m_host.m_touchPoint = new FCPoint(e.Touches[0].X, e.Touches[0].Y);
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = 1;
                if (m_isDoubleClickLeft)
                {
                    newTouchInfo.m_clicks = 2;
                    m_isDoubleClickLeft = false;
                }
                else
                {
                    newTouchInfo.m_clicks = 1;
                }
                m_native.onMouseUp(newTouchInfo);
            }
        }

        /// <summary>
        /// 触摸开始事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FCUIView_StartInteraction(object? sender, TouchEventArgs e)
        {
            m_host.m_touchPoint = new FCPoint(e.Touches[0].X, e.Touches[0].Y);
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                if (DateTime.Now - m_lastClickLeft < new TimeSpan(10000 * 300))
                {
                    newTouchInfo.m_clicks = 2;
                    m_isDoubleClickLeft = true;
                }
                else
                {
                    newTouchInfo.m_clicks = 1;
                }
                FCView lastFocusedView = m_native.getFocusedView();
                m_native.onMouseDown(newTouchInfo);
                FCView focusedView = m_native.getFocusedView();
                if (focusedView is FCTextBox)
                {
                    showTextBox(focusedView, lastFocusedView);
                }
                else
                {
                    hideTextBox(focusedView);
                }
                m_lastClickLeft = DateTime.Now;
            }
        }

        /// <summary>
        /// 显示文本框
        /// </summary>
        /// <param name="view"></param>
        /// <param name="lastView"></param>
        public void showTextBox(FCView view, FCView lastView)
        {
            if (m_textBox != null && m_editingTextBox != null)
            {
                if (lastView != null && lastView != view && lastView is FCTextBox)
                {
                    lastView.setText(m_textBox.Text);
                    lastView.invalidate();
                }
            }
            int width = (int)Width, height = (int)Height;
            m_editingTextBox = view;
            int clX = m_native.clientX(view);
            int clY = m_native.clientY(view);
            double scaleFactorX = 0, scaleFactorY = 0;
            m_native.getPaint().getScaleFactor(ref scaleFactorX, ref scaleFactorY);
            AbsoluteLayout.SetLayoutBounds(m_textBox, new Rect(clX * scaleFactorX, clY * scaleFactorY, view.getWidth() * scaleFactorX, view.getHeight() * scaleFactorY));
            m_textBox.Text = view.getText();
            MauiPaint mauiPaint = m_native.getPaint() as MauiPaint;
            m_textBox.BackgroundColor = mauiPaint.getMauiColor(view.getBackColor());
            m_textBox.TextColor = mauiPaint.getMauiColor(view.getTextColor());
            FCFont font = view.getFont();
            String fontFamily = font.m_fontFamily;
            if (fontFamily == "Default")
            {
                fontFamily = MyColor.getSystemFont();
            }
            float fontSize = font.m_fontSize;
            if (scaleFactorX != 1 || scaleFactorY != 1)
            {
                fontSize = (float)(Math.Min(scaleFactorX, scaleFactorY) * fontSize);
            }
            m_textBox.FontSize = fontSize;
            m_textBox.FontFamily = fontFamily;
            m_textBox.IsVisible = true;
            m_textBox.Focus();
        }

        /// <summary>
        /// 隐藏文本框
        /// </summary>
        /// <param name="view"></param>
        public void hideTextBox(FCView view)
        {
            if (m_textBox != null && m_editingTextBox != null)
            {
                m_editingTextBox.setText(m_textBox.Text);
                m_textBox.IsVisible = false;
                m_editingTextBox = null;
                m_native.invalidate();
            }
        }

        /// <summary>
        /// 方法库
        /// </summary>
        public FCNative m_native;

        /// <summary>
        /// 设备对接
        /// </summary>
        public MauiHost m_host;

        /// <summary>
        /// Xml解析
        /// </summary>
        public MainFrame m_xml;

        /// <summary>
        /// 加载数据
        /// </summary>
        public void onLoad()
        {
            m_xml = new MainFrame();
            m_xml.createNative();
            m_native = m_xml.getNative();
            m_native.setPaint(new MauiPaint());
            m_host = new MauiHost();
            m_host.setView(this);
            m_native.setHost(m_host);
            m_host.setNative(m_native);
            m_native.setSize(new FCSize(800, 700));
            string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
        <html xmlns=""facecat"">
        <body>
            <div bordercolor=""none"" name=""divInner"" dock=""fill"">
                <div type=""tab"" dock=""fill"" selectedindex=""0"" backcolor=""none"" bordercolor=""none""
                name=""tabFunc"">
                    <div type=""tabpage"" text=""主界面"" name=""divMain"" backcolor=""none"" bordercolor=""none"">
                    <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                            size=""610,610"" candragsplitter=""true"" splitterposition=""610,1"" name=""divCodingRight2"">
                            <div type=""custom"" cid=""securitylist"" name=""gridStocks"" headerheight=""30"" dock=""fill""
                                gridlinecolor=""none"" bordercolor=""none"" showvscrollbar=""true"" showhscrollbar=""true""
                                allowpreviewsevent=""true"" allowdragscroll=""true"">
                                <tr>
                                    <th name=""colP0"" text=""序"" width=""40"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                    <th name=""colP1"" text=""代码"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                    <th name=""colP2"" text=""名称"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                    <th name=""colP3"" text=""现价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP4"" text=""涨幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP5"" text=""涨跌"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP9"" text=""总量"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP10"" text=""总额"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP11"" text=""量比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP12"" text=""PE动"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP13"" text=""振幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP14"" text=""换手率"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP15"" text=""总市值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP16"" text=""流值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP17"" text=""行业分类板块"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""center""/>
                                    <th name=""colP18"" text=""涨停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP19"" text=""跌停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP20"" text=""金比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP21"" text=""涨跌比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP22"" text=""涨速"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP23"" text=""净资产收益率"" width=""100"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    <th name=""colP24"" text=""自设指标"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                        bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                </tr>
                            </div>
                            <div type=""splitlayout"" layoutstyle=""toptobottom"" bordercolor=""none"" dock=""fill""
                                size=""400,400"" candragsplitter=""true"" splitterposition=""400,1"">
                                <div type=""splitlayout"" layoutstyle=""righttoleft"" bordercolor=""none"" dock=""fill""
                                    size=""400,400"" candragsplitter=""true"" splitterposition=""200,1"">
                                    <div type=""custom"" cid=""latestdiv"" name=""divLatest"" />
                                    <div type=""custom"" cid=""mychart"" name=""mainChart1"" cycle=""0"" />
                                </div>
                                <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                                    size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                    <div type=""custom"" cid=""mychart"" name=""mainChart2"" cycle=""5"" />
                                    <div type=""custom"" cid=""mychart"" name=""mainChart3"" cycle=""1440"" />
                                </div>
                            </div>
                        </div>
                        </div>
                        <div type=""tabpage"" text=""多K线"" name=""divMulti"" backcolor=""none"" bordercolor=""none"">
                            <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""133,1"">
                                <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                    dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                        <div type=""custom"" cid=""mychart"" name=""chart1"" cycle=""1"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                        <div type=""custom"" cid=""mychart"" name=""chart2"" cycle=""5"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                    </div>
                                    <div type=""custom"" cid=""mychart"" name=""chart3"" cycle=""10"" nativerefresh=""true"" candledivpercent=""1""
                                        voldivpercent=""0"" />
                                </div>
                                <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                    dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                            <div type=""custom"" cid=""mychart"" name=""chart4"" cycle=""15"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                            <div type=""custom"" cid=""mychart"" name=""chart5"" cycle=""20"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                        </div>
                                        <div type=""custom"" cid=""mychart"" name=""chart6"" cycle=""30"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                    </div>
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                            <div type=""custom"" cid=""mychart"" name=""chart7"" cycle=""1440"" nativerefresh=""true""
                                                candledivpercent=""1"" voldivpercent=""0"" />
                                            <div type=""custom"" cid=""mychart"" name=""chart8"" cycle=""10080"" nativerefresh=""true""
                                                candledivpercent=""1"" voldivpercent=""0"" />
                                        </div>
                                        <div type=""custom"" cid=""mychart"" name=""chart9"" cycle=""43200"" nativerefresh=""true""
                                            candledivpercent=""1"" voldivpercent=""0"" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div type=""tabpage"" text=""输入框"" name=""divTextBox"" backcolor=""none"" bordercolor=""none"">
                            <input type=""text"" name=""TextBox"" location=""25,25"" size=""100,20""  />
                            <input type=""text"" name=""TextBox2"" location=""25,65"" size=""100,20""  />
                            <input type=""text"" name=""TextBox3"" location=""25,106"" size=""100,20""  />
                        </div>
                    </div>
                </div>
        </body>
        </html>
        ";
            m_xml.loadXml(xml, null);
            m_native.update();
            GraphicsDrawable drawable = new GraphicsDrawable();
            Drawable = drawable;
            drawable.m_view = this;
        }
    }

    /// <summary>
    /// 重绘类
    /// </summary>
    public class GraphicsDrawable : IDrawable
    {
        /// <summary>
        /// 视图
        /// </summary>
        public FCUIView m_view;

        /// <summary>
        /// 绘制方法
        /// </summary>
        /// <param name="canvas"></param>
        /// <param name="dirtyRect"></param>
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            int width = (int)m_view.Width, height = (int)m_view.Height;
            FCSize oldSize = m_view.m_native.getSize();
            if (oldSize.cx != width || oldSize.cy != height)
            {
                m_view.m_native.setSize(new FCSize(width, height));
                m_view.m_native.update();
            }
            MauiPaint mauiPaint = m_view.m_native.getPaint() as MauiPaint;
            mauiPaint.m_canvas = canvas;
            double scaleFactorX = 1, scaleFactorY = 1;
            mauiPaint.getScaleFactor(ref scaleFactorX, ref scaleFactorY);
            FCRect clipRect = new FCRect((int)(dirtyRect.Left * scaleFactorX), (int)(dirtyRect.Top * scaleFactorY), (int)(dirtyRect.Right * scaleFactorX), (int)(dirtyRect.Bottom * scaleFactorY));
            mauiPaint.beginPaint(0, clipRect, clipRect);
            m_view.m_native.onPaint(clipRect);
            mauiPaint.endPaint();
        }
    }
}
